package com.xyz.graph.util;

import com.xyz.graph.entity.GraphEdge;
import com.xyz.graph.entity.GraphVertex;
import org.apache.commons.configuration.MapConfiguration;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.janusgraph.core.ConfiguredGraphFactory;
import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphFactory;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


public class GraphUtil {

    public JanusGraphFactory.Builder config = null;
    public JanusGraph graph = null;

    //此配置备用
    public ConfiguredGraphFactory configuredGraphFactory = null;

    public JanusGraph getGraphMgnt(){
         JanusGraphFactory.Builder config = null;
         JanusGraph graph = null;

         return graph;
    }

    public JanusGraph buildGraphMgntByFile(String filePath) {
        InputStream file = null;
        try {
            file = (null == filePath) ? this.getClass().getResourceAsStream("/janusgraph.properties")
                    : new FileInputStream(filePath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Map<String, Object> properties = new HashMap<String, Object>();
        try {
            Properties p = new Properties();
            p.load(file);
            p.forEach((key, value) -> {
                properties.put((String) key, value);
                System.out.println(key + ":::" + value);
            });
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        config = JanusGraphFactory.build();
        if (null != properties) {
            properties.forEach((key, value) -> config.set(key, value));
        }
        graph = config.open();
        return graph;
    }

    //图数据库的配置文件未采用此方式，暂时弃用，后续优化
    public ConfiguredGraphFactory buildConfiguredGraphFactoryByFile(String filePath) {
        InputStream file = null;
        try {
            file = (null == filePath) ? this.getClass().getResourceAsStream("/janusgraph-configration.properties")
                    : new FileInputStream(filePath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Map<String, Object> properties = new HashMap<String, Object>();
        try {
            Properties p = new Properties();
            p.load(file);
            p.forEach((key, value) -> {
                properties.put((String) key, value);
                System.out.println(key + ":::" + value);
            });
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        configuredGraphFactory.createConfiguration(new MapConfiguration(properties));
        return configuredGraphFactory;
    }

    public static GraphVertex convert(Vertex vertex) {
        GraphVertex graphVertex = new GraphVertex();
        graphVertex.setId(vertex.id().toString());
        graphVertex.setLabel(vertex.label());
        return graphVertex;
    }

    public static GraphEdge convert(Edge edge) {
        GraphEdge graphEdge = new GraphEdge();
        graphEdge.setId(edge.id().toString());
        graphEdge.setLabel(edge.label());

        Vertex inVertex = edge.inVertex();
        Vertex outVertex = edge.outVertex();
        GraphVertex inGraphVertex = convert(inVertex);
        GraphVertex outGraphVertex = convert(outVertex);
        graphEdge.setSource(outGraphVertex);
        graphEdge.setTarget(inGraphVertex);
        return graphEdge;
    }

}
